<template>
  <!-- 招聘详情 -->
  <Top-Page>
    <view class="nav-wrap">
      <cu-custom bgColor="bg-gradual-white-3" :isBack="true">
        <template #backText></template>
        <template #content>
          <view class="nav-content__item">招聘详情</view>
        </template>
        <template #right>
          <image class="more" src="../../../static/image/common/w-more.png" mode="widthFix" />
        </template>
      </cu-custom>
    </view>

    <view class="content">
      <view class="title-salary">
        <view class="title">
          {{ data.name }}
        </view>
        <view class="salary">{{ data.salary_min }}-{{ data.salary_max }}</view>
      </view>

      <view class="info-1">
        <view class="info__item">{{ data.province }}·{{ data.city }}</view>
        <template v-for="item in data.label">
          <view class="info__item">
            {{ item }}
          </view>
        </template>
      </view>

      <view class="info-2">
        <view class="info__item">
          {{ data.company }}
        </view>
        <view class="info__item">
          {{ data.company_size }}
        </view>
      </view>

      <view class="br"></view>

      <view class="title">职位描述</view>

      <view class="label">
        <view class="label-item" v-for="item in data.label">
          {{ item }}
        </view>
      </view>

      <view class="describe" v-html="data.content"></view>

      <view class="link-up-wrap" @click="toChat">沟通</view>
    </view>
  </Top-Page>
</template>

<script lang="ts" setup>
import recruitApi from "@/api/recruit.js"; //招聘相关API
import { reactive } from "vue";
import { onLoad } from "@dcloudio/uni-app";
// import { findArray } from "@/utils/utils.js";
import util from "@/utils/utils.js";

import { useMsgStore } from "@/store/message";
import pinia from "@/store/index";
const msgStore = useMsgStore(pinia);

const data: any = reactive({});

onLoad((param: any) => {
  init(param.id);
});

function init(id) {
  recruitApi
    .detail({
      id: id
    })
    .then((res) => {
      Object.assign(data, res.data);
    });
}

const toChat = () => {
  const info = data.company_user;

  let str = `?id=${info.user_id}`;

  if (!info.is_friend) {
    info["is_group"] = 0;
    info["id"] = info.user_id;
    str += `&type=casual&realname=${info.realname}&group_id=group_recruit`;

    let contacts = msgStore.contacts;
    let contact = util.findArray(contacts, info.user_id);

    if (contact) {
      contact.lastContent = "";
      contact.lastSendTime = "";
      contact.type = "text";
      contact.unread += 0;
      // 更新联系人信息
      msgStore.updateContacts(contact);
    } else {
      let newContact = {
        id: info.user_id,
        displayName: info.realname,
        avatar: info.avatar,
        lastContent: "",
        lastSendTime: "",
        is_group: 0,
        unread: 0,
        is_top: 0,
        dep_id: 0
      };

      newContact["group_id"] = "group_recruit";
      newContact["tempChat"] = true;

      contacts.unshift(newContact);
      msgStore.initContacts(contacts);

      // this.$api.
    }

    msgStore.catchSocketAction(info);
  }

  uni.navigateTo({
    url: `/pages/message/chat?${str}`
  });
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
